package com.mall.dao;

import com.mall.domain.Permission;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 权限的持久层接口
 * @author :liangY
 * @date : 2020-12-28 15:19
 */
@Repository
public interface IPermissionDao {

    /**
     * 查询权限父节点
     * @return
     */
    @Results(id = "permissionMap",
            value = {
                    @Result(id = true,property = "id",column = "id"),
                    @Result(property = "parentId", column = "parent_id"),
                    @Result(column = "id",
                            property = "children",
                            many = @Many(select = "com.mall.dao.IPermissionDao.listChild",fetchType = FetchType.LAZY)
                    )
            })
    @Select("select * from tb_permission where parent_id=0 and status=1")
    List<Permission> getTree();

    /**
     * 查询权限子节点
     * @param parentId
     * @return
     */
    @ResultMap("permissionMap")
    @Select("select * from tb_permission where parent_id=#{parentId} and status=1")
    List<Permission> listChild(@Param("parentId")int parentId);

    /**
     * 查询权限集合
     * @return
     */
    @Results(id = "permissionList",
            value = {
                    @Result(id = true,property = "id",column = "id"),
                    @Result(property = "parentId", column = "parent_id")
            })
    @Select("select * from tb_permission where status=1")
    List<Permission> getList();

    /**
     * 查询权限父节点
     * @param roleId
     * @return
     */
    @Results(id = "permissionMap2",
            value = {
                    @Result(id = true,property = "id",column = "id"),
                    @Result(property = "parentId", column = "parent_id"),
                    @Result(column = "id",
                            property = "children",
                            many = @Many(select = "com.mall.dao.IPermissionDao.listChild",fetchType = FetchType.LAZY)
                    )
            })
    @Select("select * from tb_permission where parent_id=0 and status=1 and id in" +
            "(select permission_id from tb_role_permission where role_id=#{roleId})")
    List<Permission> getTree2(Integer roleId);

}
